# -*- coding: utf-8 -*-
import scrapy
import os
import time
from scrapy.http import Request
from scrapy.conf import settings

class TestipSpider(scrapy.Spider):
    name = 'testip'
    handle_httpstatus_list = [404, 501,502,503]
    allowed_domains = ['shudong.phpcool.cn']
    start_urls = ['http://shudong.phpcool.cn/show_ip']

    def parse(self, response):
        print(settings.get('HTTP_PROXY'))
        if response.status in self.handle_httpstatus_list:
           print("404, 501, 502, 503,403")
           yield Request(url=response.url, callback=self.parse, dont_filter=True)
        ip = response.css('h1::text').extract_first()
        useragent = response.css('h2::text').extract_first()

        try:
            print(ip)
            print(useragent)
            # os.system("""(echo authenticate '"password"'; echo signal newnym; echo \
            #                                quit) | nc localhost 9050""")
        except:
            print("get new ip from tor faith...")
        time.sleep(3)
        yield Request(url=response.url, callback=self.parse, dont_filter=True)
        #  print("response.text"+response.text)
        # time.sleep(2)
        # print(response.text)
        # ip = response.css('pre::text').extract_first()
        # if ip:
        #    print("ip-----------:"+ip)
        pass
